<?php
namespace app\admin\controller;

use app\admin\model\Caser;
use app\admin\model\Common;
use think\Db;

class Partner extends Base
{

    public function designlst ()
    {
        //获取用户信息
        $case = new Caser();
        $casies = $case->table('d_partner');

        $title = input('title');
        if (isset($title)) {
            $casies->where('company', 'like', "%{$title}%");
        }
        $casies->where('type', 2);
        $resu = $casies->order('id','desc')->paginate(10);
        $page = $resu->render();
        $this->assign('casies', $resu);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function designadd ()
    {
        if ($this->request->isPost()){
            $this->get_add();
            $this->success('新增成功', 'partner/designlst');
        }else{
            return $this->fetch();
        }
    }

    public function manufactureadd ()
    {
        if ($this->request->isPost()){
            $this->get_add();
            $this->success('新增成功', 'partner/manufacturelst');
        }else{
            return $this->fetch();
        }
    }

    public function manufactureedit ()
    {
        if ($this->request->isPost()){
            $this->get_edit();
        }else{
            $id = input('id');
            if (isset($id) && is_numeric($id)) {
                $case = Db::table('d_partner')->find($id);
            }

            if (empty($case)) {
                return $this->error('没有找到该数据');
            }

            $this->assign('case',$case);
            return $this->fetch();
        }
    }

    public function designdestroy() {
        $id = input('id');
        if (isset($id) && is_numeric($id)) {
            Db::table('d_partner')->delete($id);
        }
        $this->success('删除成功', 'partner/designlst');
    }

    public function manufacturedestroy() {
        $id = input('id');
        if (isset($id) && is_numeric($id)) {
            Db::table('d_partner')->delete($id);
        }
        $this->success('删除成功', 'partner/manufacturelst');
    }

    public function manufacturelst ()
    {
        //获取用户信息
        $case = new Caser();
        $casies = $case->table('d_partner');

        $title = input('title');
        if (isset($title)) {
            $casies->where('company', 'like', "%{$title}%");
        }
        $casies->where('type', 1);
        $resu = $casies->order('id','desc')->paginate(10);
        $page = $resu->render();
        $this->assign('casies', $resu);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //处理add提交数据
    public function get_add(){
        $datas = input('post.');
        $datas['updated_at'] = date('Y-m-d H:i:s');
        //重组数组数据
        $cl = new Caser();
        $resu = $cl->table('d_partner')->insert($datas);

        if(!$resu) {
            $this->error('添加错误');
        }
    }

    //处理add提交数据
    public function get_edit(){
        $datas = input('post.');
        $id = $datas['id'];
        unset($datas['id']);

        $flag = true;
        if (isset($id) && is_numeric($id)) {
            Db::table('d_partner')->where('id', $id)->update($datas);
            $flag = false;
        }

        $this->redirect('/admin/partner/manufactureedit?id='.$id);
    }
}


